#include "TrainBoardManager.h"

/* CONSTRUCTOR */
TrainBoardManager::TrainBoardManager()
{
  m_powerManager = new PowerManager();
  m_communication = Communication::add(&Serial, this);
}

/**
* \brief Analyse a command received over serial port
* \param command
* \return none
*/
void TrainBoardManager::commandReceived(Command* command)
{
  byte commandType = command->getType();
  byte commandDeviceId = command->getDeviceId();
  byte commandValue = command->getValue();
  switch (commandType)
  {
    case CommandType::TRAIN_SPEED_MOVE_FORWARD_UPDATE:
      m_powerManager->moveForward(commandValue);
      m_communication->sendCommand(new Command(CommandType::TRAIN_SPEED_UPDATE_ACK, commandDeviceId, CommandStatus::ACCEPTED));
      break;
      
    case CommandType::TRAIN_SPEED_MOVE_BACKWARD_UPDATE:
      m_powerManager->moveBackward(commandValue);
      m_communication->sendCommand(new Command(CommandType::TRAIN_SPEED_UPDATE_ACK, commandDeviceId, CommandStatus::ACCEPTED));
      break;
      
    case CommandType::TRAIN_SPEED_STOP_UPDATE:
      m_powerManager->stop();
      m_communication->sendCommand(new Command(CommandType::TRAIN_SPEED_UPDATE_ACK, commandDeviceId, CommandStatus::ACCEPTED));
      break;
      
    case CommandType::TRAIN_SMOKE_UPDATE:
      break;
      
    default:
      break;
  }
}
